/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.error;

import com.wttech.dzzf.process.dao.DataProcessFinalDao;
import com.wttech.dzzf.process.service.base.BaseInfoService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

/**
 * 填充证件号信息
 *
 * @author MaShibo
 * @date 2017/12/5.
 */
@Service
public class FillCertNoService {

	private static final Logger LOGGER = LoggerFactory.getLogger(FillCertNoService.class);

	private static final String FILE_PATH = "F:\\Users\\MaShibo\\Desktop\\中行第二轮自测完善\\missingAgentInfo.txt";

	@Resource
	private DataProcessFinalDao dataProcessFinalDao;

	@Resource
	private BaseInfoService baseInfoService;


	@Resource
	private ThreadPoolTaskExecutor executor;

	private void process() {
		List<Future<Void>> futures = new ArrayList<>();
		try (BufferedReader reader = new BufferedReader(new FileReader(new File(FILE_PATH)))) {
			reader.lines().forEach(line -> futures.add(executor.submit(() -> processTextLine(line))));
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		baseInfoService.getFutureResult(futures);
		LOGGER.info("执行完毕");
		executor.shutdown();
	}

	private Void processTextLine(String line) {
		String[] entries = StringUtils.split(line, "\t");
		if (entries == null || entries.length != 4) {
			LOGGER.info("异常数据行【{}】", line);
			return null;
		}
		try {
			String userType = entries[0];
			String userIdType = entries[1];
			String userIdNum = entries[2];
			String ecardNo = entries[3];
			int num = dataProcessFinalDao.updateCertInfos(Integer.parseInt(StringUtils.trim(userType)),
					Integer.parseInt(StringUtils.trim(userIdType)), StringUtils.upperCase(userIdNum), StringUtils.substring(ecardNo, 4));
			if (LOGGER.isInfoEnabled()) {
				LOGGER.info("更新{}", num > 0 ? "成功" : "失败");
			}
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		return null;
	}

	private void processMissingAgengInfo() {
		List<Future<Integer>> futures = new ArrayList<>();
		try (BufferedReader reader = new BufferedReader(new FileReader(new File(FILE_PATH)))) {
			reader.lines().forEach(line -> futures.add(executor.submit(() -> processAgentTextLine(line))));
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		baseInfoService.getFutureResult(futures);
		LOGGER.info("执行成功！");
		executor.shutdown();
	}

	private Integer processAgentTextLine(String line) {
		String[] entries = StringUtils.split(line, "\t");
		if (entries == null || entries.length != 7) {
			LOGGER.info("异常数据行【{}】", line);
			return null;
		}
		int num = 0;
		try {
			int userIdType = Integer.parseInt(StringUtils.trim(entries[0]));
			String userIdNum = StringUtils.trim(entries[1]).toUpperCase();
			String agentName = StringUtils.trim(entries[2]);
			String agentIdNum = StringUtils.trim(entries[3]).toUpperCase();
			int agentIdType = Integer.parseInt(StringUtils.trim(entries[4]));
			String tel = StringUtils.trim(entries[5]);
			String ecardNo = StringUtils.trim(entries[6]).substring(4);
			num = dataProcessFinalDao.updateAgentInfos(userIdType, userIdNum, agentName, agentIdNum, agentIdType, tel, ecardNo);
			LOGGER.info("更新成功！");
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		return num;
	}


	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		FillCertNoService service = context.getBean(FillCertNoService.class);
		service.process();
		service.processMissingAgengInfo();
	}
}
